package com.blackfish.user;

import com.blackfish.common.model.User;

import java.util.List;

public class UserService {
    static User dao = new User();

    public User getById(int id) {
        return dao.findById(id);
    }

    public List<User> getAllView() {
        return dao.find("select * from user");
    }

    public boolean getByName(String name) {
        if (dao.get("name", name)) {
            return true;
        }
        return false;
    }


    public boolean create(String name, String phone, String pwd) {
        if (this.getByName(name)) {
            return false;
        }
        boolean isSave = dao.set("name", name).set("phone", phone).set("password", pwd).save();
        return isSave;
    }

    public boolean create(String name, String phone, String pwd, String email) {
        boolean isSave = dao.set("name", name).set("phone", phone).set("password", pwd).save();
        return isSave;
    }

    public User getByName(String name, String pwd) {
        User user = dao.findFirst("select * from user where name=\"" + name + "\"");
        if(user==null){
            return null;
        }
        if (user.getPassword().equals(pwd)) {
            return user;
        }
        return null;
    }


}
